import Vue from 'vue'
// 导入vue-router插件
import VueRouter from 'vue-router'

// 2.注册vue-router插件
Vue.use(VueRouter)

// 3.定义路由表
// 一个对象就是一个路由规则
const routes = [//定义多个一级路由
  {
    path: '/index',//路由地址
    name: 'index',//路由名字
    meta: { title: 'index' },
    component: () => import('../views/Index.vue'),//动态加载(懒加载/延迟加载)
    children: [//嵌套路由( 二级路由 )
      {
        path: '/index/home',
        name: 'home',
        meta: { title: '首页' }, //路由元信息( 会永远保存在当前路由对象身上 )
        component: () => import('../views/Index/Home.vue'),//动态加载(懒加载/延迟加载)
      },
      {
        path: '/index/order',
        name: 'order',
        meta: { title: '订单' },
        component: () => import('../views/Index/Order.vue'),
      },
      {
        path: '/index/mine',
        name: 'mine',
        meta: { title: '我的' },
        component: () => import('../views/Index/Mine.vue')
      },
      {
        path: '/index/tuan',
        name: 'tuan',
        meta: { title: '爆爆团' },
        component: () => import('../views/Index/Tuan.vue')
      },
      //二级路由的重定向
      {
        path: '/index',
        redirect: '/index/home'
      },
      //404路由
      {
        path: '*',
        component: () => import('../views/NotFound.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'about',
    meta: { title: '登陆' },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta: { title: '注册' },
    component: () => import('../views/Register.vue'),
  },
  {
    // 路由地址中带有 :xxx 的路由地址 都属于动态路由
    // path: '/detail/:name', // :name 是个占位符, 实际在跳路由的时候,会动态拼接参数 例如: '/detail/'+100
    // path: '/detail/:id',
    path: '/detail/:id',
    name: 'detail',
    meta: { title: '详情' },
    component: () => import('../views/Detail.vue'),

  },
  {
    path: '/collect',
    name: 'collect',
    component: () => import('../views/Collect.vue')
  },
  {
    path: '/addaddress',
    name: 'addaddress',
    meta: { title: '新增地址' },
    component: () => import('../views/AddAddress.vue')
  },
  {
    path: '/mineaddress',
    name: 'mineaddress',
    meta: { title: '我的收货地址' },
    component: () => import('../views/MineAddress.vue')
  },
  {
    path: '/shippingaddress',
    name: '/shippingaddress',
    meta: { title: '收货地址(修改/删除)' },
    component: () => import('../views/ShippingAddress.vue')
  },
  // search
  {
    path: '/inputsearch',
    name: '/inputsearch',
    meta: { title: '搜索' },
    component: () => import('../views/InputSearch.vue')
  },
  //一级路由重定向
  {
    path: '/',
    redirect: '/index'
  },
  //404路由
  {
    path: '*',
    component: () => import('../views/NotFound.vue')
  }
]

// 4.实例化路由对象
const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 设置实例方法(导航守卫方法)

// 路由的全局前置守卫方法
router.beforeEach((to, from, next) => {
  //参数一: to 新的路由对象
  //参数二: from 旧的路由对象
  //参数三: next 路由控制方法, 调用该方法则允许路由跳转, 未调用该方法则不允许路由跳转

  // 动态更新网页标题
  document.title = to.meta.title;

  // 获取localStorage中的登录凭证
  var token = localStorage.getItem('token');


  // 只有在登录后才可以跳转到订单页和我的页面
  if (to.path == '/index/order' || to.path == '/index/mine') {
    if (token) {//已经登录
      next();
    } else {//未登录
      next('/login');
    }
  } else {//其他页面( 非订单页 )
    next();//必须手动调用该函数, 否则无法完成路由跳转
  }
})

// 5.导出路由对象
export default router
